/* 
 * Copyright (c) 2009, Giora Kosoi giorak@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Giora Kosoi ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Giora Kosoi BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sparrow/variant.h>
#include <sparrow/errors.h>

std::vector<std::string> init_types(const char *t1, ...)
{
    std::vector<std::string> result;
    va_list ap;
    va_start(ap, t1);
    const char *elem = t1;
    do
    {
        result.push_back(elem);
        elem = va_arg(ap, const char *);
    }
    while(elem != NULL);
    va_end(ap);
    return result;
}

static std::vector<std::string> vartypes = 
    init_types(
        "VT_NULL",
        "VT_INT32",
        "VT_INT64",
        "VT_IPV4",
        "VT_VARIANT_SET",
        "VT_PTR",
        "VT_STR",
        NULL);
static std::string unknown_type = "unknown_type";

const std::string &Variant::vartype_to_str(VariantType vt)
{
    if(vt > 0 && vt <= vartypes.size())
    {
        return vartypes[vt - 1];
    }
    return unknown_type;
}

Variant::Variant (VariantType vt)
    :   vt_(vt)
{
}

Variant::~Variant ()
{
}

int32_t Variant::int32() const
{
    throw_not_implemented("int32() const", __FILE__, __LINE__);
    return 0;
}

void Variant::int32(int32_t value)
{
    throw_not_implemented("int32(int32_t)", __FILE__, __LINE__);
}

uint32_t Variant::uint32() const
{
    throw_not_implemented("uint32() const", __FILE__, __LINE__);
    return 0;
}

void Variant::uint32(uint32_t value)
{
    throw_not_implemented("uint32(uint32_t)", __FILE__, __LINE__);
}

int64_t Variant::int64() const
{
    throw_not_implemented("int64() const", __FILE__, __LINE__);
    return 0;
}

void Variant::int64(int64_t value)
{
    throw_not_implemented("int64(int64_t)", __FILE__, __LINE__);
}

uint64_t Variant::uint64() const
{
    throw_not_implemented("uint64() const", __FILE__, __LINE__);
    return 0;
}

void Variant::uint64(uint64_t value)
{
    throw_not_implemented("uint64(uint64_t)", __FILE__, __LINE__);
}

const in_addr &Variant::ipv4() const
{
    throw_not_implemented("ipv4() const", __FILE__, __LINE__);
    return *(const in_addr *)NULL;
}

void Variant::ipv4(const in_addr &value)
{
    throw_not_implemented("ipv4(const in_addr &)", __FILE__, __LINE__);
}

const VariantSet* Variant::variant_set() const
{
    throw_not_implemented("variant_set() const", __FILE__, __LINE__);
    return NULL;
}

VariantSet* Variant::variant_set()
{
    throw_not_implemented("variant_set()", __FILE__, __LINE__);
    return NULL;
}


void Variant::variant_set(VariantSet *value)
{
    throw_not_implemented("variant_set(VariantSet *)", __FILE__, __LINE__);
}

const Octets &Variant::octets() const
{
    throw_not_implemented("octets() const", __FILE__, __LINE__);
    return *(Octets *)NULL;
}

void Variant::octets(const Octets &value)
{
    throw_not_implemented("octets(const Octets &)", __FILE__, __LINE__);
}

void Variant::octets(const Octets::const_iterator &begin, const Octets::const_iterator &end)
{
    throw_not_implemented("octets(Octets::const_iterator, Octets::const_iterator)", __FILE__, __LINE__);
}

void Variant::octets(const uint8_t *value, size_t value_len)
{
    throw_not_implemented("octets(const uint8_t *, size_t)", __FILE__, __LINE__);
}

void Variant::swap(Octets &value)
{
    throw_not_implemented("swap(Octets &)", __FILE__, __LINE__);
}

const std::string &Variant::str() const
{
    throw_not_implemented("str() const", __FILE__, __LINE__);
    return *(std::string *)NULL;
}

const std::string &Variant::pstr() const
{
    throw_not_implemented("pstr() const", __FILE__, __LINE__);
    return *(std::string *)NULL;
}


void Variant::str(const std::string &value)
{
    throw_not_implemented("str(const std::string &)", __FILE__, __LINE__);
}

void Variant::swap(std::string &value)
{
    throw_not_implemented("swap(std::string &)", __FILE__, __LINE__);
}

void Variant::ptr(void *p, deletef del, bool own)
{
    throw_not_implemented("ptr(void *, deletef, bool)", __FILE__, __LINE__);
}

void *Variant::ptr() const
{
    throw_not_implemented("ptr() const", __FILE__, __LINE__);
}

Variant *Variant::clone() const
{
    throw_not_implemented("clone() const", __FILE__, __LINE__);
    return NULL;
}

void Variant::throw_not_implemented(const char *what, const char *file, int line) const
{
    throw_error<std::logic_error>(__FILE__, __LINE__,
            "function [%s] is not implemented for class [%s]",
            what, demangle(typeid(*this).name()).c_str());
}

Variant::VariantType Variant::type() const
{
    return vt_;
}

